const mysql = require('mysql')
const config = {
    database: 'notebook',
    user: 'root',
    password: 'remover',
    timezone: 'SYSTEM'
}
const NoteService = {
    getAllNotes: (uid) => {
        let sqlParams = [uid];
        sql = 'select * from t_notes where bid = ? ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('共查到' + res.length + '条数据');
                            console.log(res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    getSelectedNote: (id) => {
        let sqlParams = [id];
        sql = 'select * from t_notes where id = ? ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('共查到' + res.length + '条数据');
                            console.log(res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    saveNoteByNid: (id, content, title, date, backgroundColor) => {
        let sqlParams = [content, title, date, backgroundColor, id];
        sql = 'update t_notes set content = ? ,title = ? ,date = ? ,backgroundColor = ? where id = ? ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            if (res.affectedRows === 1) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    newNoteByUid: (bid, date) => {
        let sqlParams = [bid, date];
        sql = 'insert into t_notes set  bid = ? ,date = ?;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('新增了' + res.length + '个笔记');
                            console.log(res);
                            if (res.insertId > 0) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    daleteNoteByNid: (id) => {
        let sqlParams = [id];
        sql = 'delete from t_notes where id = ? ;'
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            if (res.affectedRows === 1) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    }
}
module.exports = NoteService